/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 *
 *           File:  main.cpp
 *    Description:
 *
 *         Author:  Aleksey Golovchenko (lexgolovchenko@gmail.com)
 *        Created:  19.03.2013 1:46:31
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include <iostream>
#include <cstdio>
#include <string>

#include "test.h"
#include "error_probability.h"
extern "C" {
	#include <quadmath.h>
}

int main(int argc, char **argv) {

	if (argc < 2) {
		std::cerr << "Usage : " << argv[0] << " config_file" << std::endl;
		std::cerr << "               or    " << std::endl;
		std::cerr << "        " << argv[0] << " -i N K pin" << std::endl;
		return -1;
	}

	// интерактивный режим
	if (std::string(argv[1]) == "-i") {
		if (argc < 5) {
			std::cerr << "Usage : " << argv[0] << " -i N K pin" << std::endl;
			return -1;
		}

		int N, K, t;
		__float128 pin;

		int res;
		res = std::sscanf(argv[2], "%d", &N);
		if (res != 1 || N <= 0) {
			std::cerr << "*** ERROR *** : wrong input param" << std::endl;
			return -1;
		}

		res = std::sscanf(argv[3], "%d", &K);
		if (res != 1 || K <= 0) {
			std::cerr << "*** ERROR *** : wrong input param" << std::endl;
			return -1;
		}

		if (K > N) {
			std::cerr << "*** ERROR *** : wrong input param" << std::endl;
			return -1;
		}

		t = (N - K) / 2;


		double dbuf;
		res = std::sscanf(argv[4], "%lf", &dbuf);
		if (res != 1 || dbuf <= 0) {
			std::cerr << "*** ERROR *** : wrong input param" << std::endl;
			return -1;
		}
		pin = dbuf;

		char outbuf[128];
		quadmath_snprintf(outbuf, sizeof outbuf, "%-#*.8Qe", 20, code_out_perr(pin, N, t));

		std::cout << "Input  : N    = " << N << std::endl;
		std::cout << "         K    = " << K << std::endl;
		std::cout << "         t    = " << t << std::endl;
		std::cout << "         pin  = " << dbuf << "\n" << std::endl;

		std::cout << "Output : Perr = " << outbuf << "\n" << std::endl;
		return 0;
	}

	if (-1 == test_start(argv[1])) {
		std::cerr << "*** ERROR *** : test termination failure." << std::endl;
		return -1;
	}

	return 0;
}



